from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext import webapp
from eutax.robopuzzle.web import util
from eutax.robopuzzle.core import database
from eutax.robopuzzle.core.parsers import MapParser
import re

class MainPage(webapp.RequestHandler):
  def get(self):
    templateValues = {'title': 'RoboPuzzle'}
    self.response.out.write(util.renderTemplate(self.request.uri, 
                                       templateValues, 
                                       'templates/main_home.html'))
    
class Maps(webapp.RequestHandler):
  def get(self):
    if not util.isLoggedIn():
      return renderNotLoggedInTemplate(self)
    templateValues = {'title': 'Maps'}
    
    mapList = memcache.get("lastCreatedMaps")
    if mapList is None:
      mapList = self.findLastCreatedMaps(50)
      memcache.add("lastCreatedMaps", mapList, 60)
    templateValues['mapList'] = mapList

    self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_maps.html'))

  def findLastCreatedMaps(self, limit):
    maps = database.Map.all()
    maps.order('-createdDate')
    limitInt = int(limit)
    if limitInt < 0:
      limitInt = 0
    elif limitInt > 50:
      limitInt = 50
    return maps.fetch(limitInt, 0)
  
class MapCreator(webapp.RequestHandler):
  
  def post(self):
    if not util.isLoggedIn():
      return renderNotLoggedInTemplate(self)
    templateValues = {'title': 'Map Creator'}
    
    self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_maps_create.html'))

  def get(self):
    if not util.isLoggedIn():
      return renderNotLoggedInTemplate(self)
    templateValues = {'title': 'Map Creator'}
      
    self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_maps_create.html'))
    
class MapPresentation(webapp.RequestHandler):
  def get(self):
    if not util.isLoggedIn():
      return renderNotLoggedInTemplate(self)
    templateValues = {'title': 'Map'}
    mapDataObject = fetchMap('.*/maps/show/(.+$)', self.request.uri)
    templateValues.update(mapDataObject)
      
    self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_maps_map_presentation.html'))

class IDE(webapp.RequestHandler):

  def get(self):
    if not util.isLoggedIn():
      return renderNotLoggedInTemplate(self)
    templateValues = {'title': 'IDE'}
    mapDataObject = fetchMap('.*/ide/map/(.+$)', self.request.uri)
    templateValues.update(mapDataObject)
      
    self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_ide.html'))

class TaskList(webapp.RequestHandler):
  def get(self):
    if not util.isLoggedIn():
      return renderNotLoggedInTemplate(self)
    templateValues = {'title': 'Task List'}
    self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_tasklist.html'))
def fetchMap(regexp, uri):
  mapDataObject = {}
  p = re.compile(regexp)
  m = p.match(uri)
  if m is not None:
      mapKey = m.group(1)
      if mapKey:
          mapDataObject = fetchMapFromKey(mapKey)
  return mapDataObject
  
def fetchMapFromKey(key):
  dataObject = {}
  try:
    map = database.Map.get(key)
    map.tiles = MapParser().getTiles(map.data)
    dataObject['map'] = map
  except db.BadKeyError, error:
    dataObject['error'] = error
  return dataObject

def renderNotLoggedInTemplate(self):
  templateValues = {'title': 'Login Required'}
  self.response.out.write(util.renderTemplate(self.request.uri,
                                           templateValues,
                                           'templates/main_login.html'))
